unit GlavnaKnjigaFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBBaseFrm, DokumentFrm, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxEdit, DB, cxDBData, cxGridBandedTableView, DBClient, MConnect,
  alBaseDataSet, cxEditRepositoryItems, cxContainer, StdActns, ActnList,
  cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBBandedTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  StdCtrls, ExtCtrls, cxDBEditRepository, ImgList, PoslovanjeSrv_TLB,
  cxDataStorage, cxExtEditRepositoryItems, alActions, kdbstruc, kdbinfo,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter;

type
  TfrmGlavnaKnjiga = class(TfrmDokument)
    grdtvMainPostavkaKonto_1: TcxGridDBBandedColumn;
    grdtvMainPostavkaKonto_2: TcxGridDBBandedColumn;
    grdtvMainPostavkaKonto_3: TcxGridDBBandedColumn;
    grdtvMainPostavkaSifrantNaziv: TcxGridDBBandedColumn;
    scnKonto: TSharedConnection;
    cdsKonto: TBaseDataSet;
    dtsrcKonto: TDataSource;
    rpstMainKontoNaziv: TcxEditRepositoryLookupComboBoxItem;
    grdtvMainPostavkaOpomba: TcxGridDBBandedColumn;
    grdtvMainPostavkaDebet: TcxGridDBBandedColumn;
    grdtvMainPostavkaKredit: TcxGridDBBandedColumn;
    rpstMainPostavkaZnesek: TcxEditRepositoryCurrencyItem;
    grdtvMainPostavkaStevilka: TcxGridDBBandedColumn;
    rpstMainPostavkaOpomba: TcxEditRepositoryTextItem;
    rpstMainPostavkaSifrantNaziv: TcxEditRepositoryTextItem;
    rpstMainPostavkaStevilka: TcxEditRepositoryMaskItem;
    grdtvMainPostavkaKontoSifra: TcxGridDBBandedColumn;
    actnKnjigaTerjatev: TReportPrint;
    actnKnjigaDolgov: TReportPrint;
    actnGlavnaKnjiga: TReportPrint;
    procedure OnGetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);
    procedure OnDblClick(Sender: TObject);
    procedure OnDataControllerSummaryDefaultGroupSummaryItemsSummary(
      ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments;
      var OutArguments: TcxSummaryEventOutArguments);
  private
    { Private declarations }
  protected
    function GetSessionParamRelations(DataSet: TDataSet): TSessionParamRelations; override;
    procedure Deinitialize; override;
    procedure Initialize; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  frmGlavnaKnjiga: TfrmGlavnaKnjiga;

implementation

uses
  MainFrm, TemeljnicaFrm;

{$R *.dfm}

constructor TfrmGlavnaKnjiga.Create(AOwner: TComponent);
begin
  inherited;
  with grdtvMain do
  begin
    OptionsView.Indicator := False;
    OptionsView.GroupFooters := gfAlwaysVisible;
    DataController.DataModeController.SyncMode := False;
  end;
end;

procedure TfrmGlavnaKnjiga.Deinitialize;
begin
  cdsKonto.Close;
  inherited;
end;

function TfrmGlavnaKnjiga.GetSessionParamRelations(DataSet: TDataSet): TSessionParamRelations;
begin
  Result := inherited GetSessionParamRelations(DataSet);
  if DataSet = cdsKonto then
  begin
    SetLength(Result, 1);
    Result[0].FieldName := 'REF_ID';
    Result[0].SessionName := 'POSLOVNO_LETO_ID';
  end;
end;

procedure TfrmGlavnaKnjiga.Initialize;
begin
  cdsKonto.Open;
  inherited;
end;

procedure TfrmGlavnaKnjiga.OnDataControllerSummaryDefaultGroupSummaryItemsSummary(
  ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments;
  var OutArguments: TcxSummaryEventOutArguments);
var
  ADebet: Double;
  AKredit: Double;
begin
  inherited;
  if Arguments.SummaryItem.ItemLink = grdtvMainOpomba then
  begin
    ADebet := ASender.DataController.Values[Arguments.RecordIndex, grdtvMainPostavkaDebet.Index];
    AKredit := ASender.DataController.Values[Arguments.RecordIndex, grdtvMainPostavkaKredit.Index];
    OutArguments.Value := ADebet - AKredit;
  end;
end;

procedure TfrmGlavnaKnjiga.OnDblClick(Sender: TObject);
var
  APoint: TPoint;
  AHitTest: TcxCustomGridHitTest;
  ARecord: TcxCustomGridRecord;
  AForm: TfrmDBBase;
  AStevilka: Variant;
begin
  GetCursorPos(APoint);
  with grdMain.FocusedView.Site do
  begin
    APoint := ScreenToClient(APoint);
    AHitTest := ViewInfo.GetHitTest(APoint);
    if AHitTest is TcxGridRecordHitTest then
    begin
      ARecord := TcxGridRecordHitTest(AHitTest).GridRecord;
      if ARecord.HasCells then
      begin
        AStevilka := VarToStrDef(ARecord.Values[grdtvMainStevilka.Index], '-1');
        AForm := (Application.MainForm as TfrmMain).ShowForm(TfrmTemeljnica) as TfrmDBBase;
        if Assigned(AForm) then
          AForm.Locate('STEVILKA', AStevilka);
      end;
    end;
  end;
end;

procedure TfrmGlavnaKnjiga.OnGetDisplayText(Sender: TcxCustomGridTableItem;
  ARecord: TcxCustomGridRecord; var AText: String);
var
  Sifra: Variant;
  Naziv: Variant;
  //Vsota: string;
  Stevilka: Variant;
  PostavkaStevilka: Variant;
begin
  inherited;
  if (Sender = grdtvMainPostavkaKonto_1) or (Sender = grdtvMainPostavkaKonto_2) or
    (Sender = grdtvMainPostavkaKonto_3) then
  begin
    Sifra := Trim(ARecord.Values[grdtvMainPostavkaKontoSifra.Index]);
    Naziv := '';
    if VarIsNull(Sifra) or VarIsEmpty(Sifra) or (Sifra = '') then
      Sifra := -1;
    if cdsKonto.Active then
      Naziv := cdsKonto.Lookup('SIFRA', Sifra, 'NAZIV');
    //Vsota := Sender.GridView.DataController.Summary.GroupSummaryText[ARecord.Index];
    //AText := Format('Konto : %0:s - %1:s %2:s', [Sifra, Naziv, Vsota]);
    AText := Format('Konto : %0:s - %1:s', [Sifra, Naziv]);
  end
  else if Sender = grdtvMainPostavkaStevilka then
  begin
    Stevilka := VarToStrDef(ARecord.Values[grdtvMainStevilka.Index], '-1');
    PostavkaStevilka := VarToStrDef(ARecord.Values[grdtvMainPostavkaStevilka.Index], '-1');
    if (PostavkaStevilka = -1) or (Stevilka = PostavkaStevilka) then
      AText := ''
    else
      AText := PostavkaStevilka;
  end;
end;

end.
